// 商品分类全局组件的小仓库
// @ts-expect-error
import {defineStore} from 'pinia'
// @ts-expect-error
import {reqC1, reqC2, reqC3} from '@/api/product/attr'
import type {CategoryResponseData} from '@/api/product/attr/type'

const useCategoryStore = defineStore('Category', {
  state: (): {
    c1Id: string
    c3Id: string
    c2Id: string
    c2Arr: string
    c1Arr: any[]
    c3Arr: string
  } => {
    return {
      // 存储一级分类的数据
      c1Arr: [],
      // 存储一级分类的ID
      c1Id: '',
      // 存储一级分类下的二级分类的数据
      c2Arr: '',
      // 存储二级分类的ID
      c2Id: '',
      // 存储一级分类下的二级分类下的三级分类的数据
      c3Arr: '',
      // 存储三级分类的ID
      c3Id: '',
    }
  },
  actions: {
    // 获取一级分类的方法
    async getC1() {
      const result: CategoryResponseData = await reqC1()
      if (result.code === 200) {
        // @ts-expect-error
        this.c1Arr = result.data
      }
    },
    async getC2() {
      // @ts-expect-error
      const result: CategoryResponseData = await reqC2(this.c1Id)
      if (result.code === 200) {
        // @ts-expect-error
        this.c2Arr = result.data
      }
    },
    async getC3() {
      // @ts-expect-error
      const result: CategoryResponseData = await reqC3(this.c2Id)
      if (result.code === 200) {
        // @ts-expect-error
        this.c3Arr = result.data
      }
    },
  },
  getters: {},
})
export default useCategoryStore
